package org.reservation.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.reservation.dao.FoodOrderItemDAO;
import org.reservation.pojo.Food;
import org.reservation.pojo.FoodOrder;
import org.reservation.pojo.FoodOrderItem;
import org.reservation.pojo.Restaurant;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class FoodOrderItemDAOHiberImpl extends HibernateDaoSupport implements FoodOrderItemDAO{

	public void add(FoodOrderItem foodOrderItem) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(foodOrderItem);
	}

	public void delete(int foodOrderItemId) {
		// TODO Auto-generated method stub
		FoodOrderItem foodOrderItem=getHibernateTemplate().get(FoodOrderItem.class,foodOrderItemId);
		getHibernateTemplate().delete(foodOrderItem);
	}

	public FoodOrderItem getById(int foodOrderItemId) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().get(FoodOrderItem.class,foodOrderItemId);
	}

	public List<FoodOrderItem> loadByOrder(String foodOrderId) {
		// TODO Auto-generated method stub
		final FoodOrder foodOrder=getHibernateTemplate().get(FoodOrder.class, foodOrderId);
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(FoodOrderItem.class);
				cri.add(Restrictions.eq("foodOrder",foodOrder));
				List<Restaurant> result = cri.list();
		return result;
	}
		});
		return list;
	}

	public List<FoodOrderItem> loadall() {
		// TODO Auto-generated method stub
		return getHibernateTemplate().find("from FoodOrderItem");
	}

	public List<FoodOrderItem> loadByRes(final int resId) {
		// TODO Auto-generated method stub
	
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(FoodOrderItem.class);
				cri.add(Restrictions.eq("resId",resId));
				List<FoodOrderItem> result = cri.list();
		return result;
	}
		});
		return list;
	}

	public void update(FoodOrderItem foodOrderItem) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(foodOrderItem);
	}

	
}
